import { createSlice } from "@reduxjs/toolkit";
import { Toast } from "antd-mobile";

export const AddPersonSlice = createSlice({
  name: "AddPersonSlice",
  initialState: {
    tickPersonList: [
      {
        id: 1,
        name: "迪丽热巴",
        code: "411381199908206666",
        state: false,
        tel: "18601962253",
      },
      {
        id: 2,
        name: "刘强东",
        code: "311381198404158888",
        state: false,
        tel: "18201962253",
      },
      {
        id: 3,
        name: "印度阿三",
        code: "011381199507233321",
        state: false,
        tel: "15601962253",
      },
      {
        id: 4,
        name: "巴铁小伙",
        code: "211021200009047893",
        state: false,
        tel: "13701962253",
      },
    ],
  },
  reducers: {
    //修改列表选中状态
    editStatus(state, { payload }) {
      const item = state.tickPersonList.find((item) => item.id === payload.id);
      if (item) {
        item.state = !payload.state;
      }
    },
    //删除列表
    delPerItem(state, { payload }) {
      state.tickPersonList = state.tickPersonList.filter(
        (item) => item.id !== payload.id
      );
    },
    //添加操作
    addPerItem(state, { payload }) {
      const item = state.tickPersonList.find((item) => item.id === payload.id);
      if (!item) {
        state.tickPersonList.push(payload);
        Toast.show("添加成功");
      } else {
        Toast.show("添加失败");
      }
    },
    //修改操作
    editPerItem(state, { payload }) {
      const item = state.tickPersonList.find((item) => item.id === payload.id);
      if (item) {
        Object.assign(item, payload);
        Toast.show("修改成功");
      }
    },
  },
});

export const { editStatus, delPerItem, addPerItem, editPerItem } =
  AddPersonSlice.actions;

export default AddPersonSlice.reducer;
